# Maintainer: Benedikt Bitterli <benedikt.bitterli@gmail.com>

_realname=rapidjson
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.1.0
pkgrel=7
pkgdesc='A fast JSON parser/generator for C++ with both SAX/DOM style API (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url='https://github.com/Tencent/rapidjson'
license=('MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-doxygen")
source=("${_realname}-${pkgver}.tar.gz::https://github.com/Tencent/rapidjson/archive/refs/tags/v${pkgver}.tar.gz"
        '0001-Use-relative-paths-for-doxygen-outputs.patch'
        '0002-Add-CMake-check-for-MinGW.patch'
        '0003-Exclude-CMakeLists.txt-from-install-targets.patch'
        '0004-CMake-Install-compiled-example-executables.patch'
        '0005-PrettyWriter.patch'
        '0006-aarch64.patch'
        '0007-fix-pkgconfig-file.patch'
        '0008-cmake-relocate-dirs.patch')
sha256sums=('bf7ced29704a1e696fbccf2a2b4ea068e7774fa37f6d7dd4039d0787f8bed98e'
            '5a838fe611b367f8939231aae1f604f83fd97108d3b2ac66def00ff1205afcf2'
            '8160d2ef142055cecb520b23cf11e90e0a6e92719427fa33755800cc748cf8be'
            '04ba9821bb09d5f035093bce09690717101f03851308aa12d498311f11024b02'
            '479b8654f0418aa89f245af4345e5df409ac5a91667815a47592da1b6d5d9eee'
            'e3efdb8263bafb3fc3af7558c7f57f9d8a3d2ab231f05c693cc615d7784cd320'
            'a2bd02572ffd4eb39d2248b3e818072d07948a65cb440f90d2fd5688e370570b'
            '3c186ea46d4ab1b67f4cd577799a059b6312dd43bf7b28e0b86c0a1bedac5cf8'
            '3e5bc65df66e77e183c8046e3133132606c969754930a999f318f5ca5c6bd190')

prepare() {
  cd "${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}/0001-Use-relative-paths-for-doxygen-outputs.patch"
  patch -p1 -i "${srcdir}/0002-Add-CMake-check-for-MinGW.patch"
  patch -p1 -i "${srcdir}/0003-Exclude-CMakeLists.txt-from-install-targets.patch"
  patch -p1 -i "${srcdir}/0004-CMake-Install-compiled-example-executables.patch"

  # Cherrypick changes from upstream:
  # https://github.com/Tencent/rapidjson/commit/1a803826f1197b5e30703afe4b9c0e7dd48074f5
  # https://github.com/Tencent/rapidjson/commit/d88be8ef1649eca4602348d1aab5c16c36f83d4f
  patch -p1 -i "${srcdir}/0005-PrettyWriter.patch"

  if [[ ${MSYSTEM_CARCH} == aarch64 ]]; then
    patch -p1 -i "${srcdir}/0006-aarch64.patch"
  fi

  patch -p1 -i "${srcdir}/0007-fix-pkgconfig-file.patch"

  # With uppercase RAPIDJSON_ variables, using
  # https://github.com/Tencent/rapidjson/commit/0bc856a747ce6b56107c29c80e5f86eb0fee151e
  patch -p1 -i "${srcdir}/0008-cmake-relocate-dirs.patch"

  find -name CMakeLists.txt | xargs sed -e 's|-Werror||' -i # Don't use -Werror
}

build() {
  [[ -d "build-${CARCH}" ]] && rm -rf "build-${CARCH}"
  mkdir "${srcdir}/build-${CARCH}" && cd "${srcdir}/build-${CARCH}"

  MSYS2_ARG_CONV_EXCL='-DCMAKE_INSTALL_PREFIX=' \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    "../${_realname}-${pkgver}"

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

package() {
  cd "${srcdir}/build-${CARCH}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --build ./ --target install

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/license.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/license.txt"
}
